package com.yunji.common.convert;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yunji.common.enums.common.NoticeTypes;
import com.yunji.common.model.domain.NoticeDO;
import com.yunji.common.model.vo.NoticeVO;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class NoticeConvert {

    public static List<NoticeVO> DOToVO(List<NoticeDO> list) {
        List<NoticeVO> voList = new ArrayList<NoticeVO>();
        for (NoticeDO noticeDO : list) {
            NoticeVO vo = new NoticeVO();
            vo.setNoticeTitle(NoticeTypes.getTitle(noticeDO.getNoticeType()));
            vo.setContent(JsonToMap(noticeDO.getNoticeContent()));
            voList.add(vo);
        }
        return voList;
    }

    public static void contentToMap(List<NoticeVO> voList){
        for (NoticeVO vo : voList) {
            vo.setContent(JsonToMap(vo.getJsonContent()));
        }
    }

    public static  Map<String, Object> JsonToMap(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> content = null;
        try {
            // 将 JSON 字符串转换为 Map<String, Object>
            content = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {
            });
        } catch (IOException e) {
            log.error("NoticeConvert DOToVO msg:{}", json, e);
        }
        return content;
    }




}
